import Taro from '@tarojs/taro'
import { H5_BASE_URL } from '@/constants/env'

const normalizeBase = () => H5_BASE_URL?.replace(/\/$/, '') || ''

export const openWebview = (path: string) => {
  const base = normalizeBase()
  const targetUrl = /^https?:\/\//i.test(path)
    ? path
    : `${base}${path.startsWith('/') ? '' : '/'}${path}`

  if (!targetUrl) {
    Taro.showToast({ title: '请先配置 H5_BASE_URL', icon: 'none' })
    return
  }

  Taro.navigateTo({
    url: `/pages/webview/index?target=${encodeURIComponent(targetUrl)}`
  })
}
